package com.service.secondapp.controller;

import com.huawei.paas.cse.tcc.annotation.TccTransaction;
import com.service.secondapp.bean.People;
import com.service.secondapp.service.PeopleService;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.List;

/**
 * Created by wjh on 2018/3/27.
 */
@RestSchema(schemaId = "people")
@RequestMapping(path = "/secondapp/people", produces = MediaType.APPLICATION_JSON)
public class PeopleController {

    private static final Logger logger = LoggerFactory.getLogger(PeopleController.class);

    @Autowired
    private PeopleService peopleService;

    @Value("${service_description.version}")
    public String version;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public Object hellotest(@RequestParam(value = "name") String name) {
        logger.info("=============== providerApp1 hello:" + name + " ===============");
        return peopleService.doRpcTest(name);
    }

    @RequestMapping(value = "/hello/{name}", method = RequestMethod.POST)
    public Object helloTestRest(@PathVariable(value = "name") String name) {
        logger.info("=============== providerApp1 hello rest:" + name + " ===============");
        return "rest hello " + name + ",version:" + version;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Object getPeopleList(){
        long currentTime = System.currentTimeMillis();
        List list = peopleService.getPeopleList();
        logger.info("耗时：" + (System.currentTimeMillis() - currentTime));
        return list;
    }

    @RequestMapping(value = "/{people_id}", method = RequestMethod.GET)
    public Object getPeopleById(@PathVariable(value = "people_id") Integer peopleId ){
        long currentTime = System.currentTimeMillis();
        People people = peopleService.getPeopleById(peopleId);
        logger.info("耗时：" + (System.currentTimeMillis() - currentTime));
        return people;
    }

    @RequestMapping(method = RequestMethod.POST)
    public Object addPeople(@RequestBody People people){
        long currentTime = System.currentTimeMillis();
        people.setStatus(0);
        peopleService.addPeople(people);
        logger.info("耗时：" + (System.currentTimeMillis() - currentTime));
        return people;
    }

    @RequestMapping(value = "/not_tcc",method = RequestMethod.POST)
    public Object addPeopleNoTcc(@RequestBody People people){
        long currentTime = System.currentTimeMillis();
        people.setStatus(0);
        peopleService.addPeopleNoTcc(people);
        logger.info("耗时：" + (System.currentTimeMillis() - currentTime));
        return people;
    }

    @RequestMapping(value="/{people_id}",method = RequestMethod.PUT)
    public Object updatePeople(@RequestBody People people){
        peopleService.updatePeople(people);
        return people;
    }

}
